﻿//----------------------------------------------------

#region 命名空间

//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 业务逻辑类 WinnerCMS_Words 的摘要说明。
    /// </summary>
    public class WordsManage
    {
        private static readonly WordsDal MyDAL = new WordsDal();

        #region 添加

        /// <summary>
        /// 添加
        /// </summary>
        public void Add(Words model, ref string strErr)
        {
            if (model == null) throw new ArgumentNullException("model");
            if (model.Find.Length == 0)
            {
                strErr = "“识别词”不能为空！";
                return;
            }

            if (model.AdminName.Length == 0)
            {
                strErr = "“管理员”不能为空！";
                return;
            }

            model.UpDateTime = DateTime.Now;
            if (model.Id == 0)
            {
                MyDAL.Add(model);
            }
            else
            {
                MyDAL.Edit(model);
            }
        }

        #endregion

        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="IDs"></param>
        /// <param name="UpdatePanel1"></param>
        public void Delete(string IDs, UpdatePanel UpdatePanel1)
        {
            if (string.IsNullOrEmpty(IDs))
            {
                MsgBox.Message("至少选择一项。", UpdatePanel1);
            }
            else
            {
                MyDAL.Delete(MyConvert.ConvertToArr(IDs));
            }
        }

        #endregion

        #region 绑定

        /// <summary>
        /// 绑定
        /// </summary>
        public IList<Words> BindList(int ban, PagingParam pp)
        {
            return MyDAL.BindList(ban, pp);
        }

        #endregion

        #region 输入内容过滤被禁止关键字

        /// <summary>
        /// 输入内容过滤被禁止关键字
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        public static string FilterBadWords(string inPut)
        {
            Dictionary<string, string> dic = MyDAL.GetFilterWords();

            StringBuilder outPut = new StringBuilder(inPut);

            foreach (KeyValuePair<string, string> kvp in dic)
            {
                if (kvp.Value.Length == 0)
                    outPut.Replace(kvp.Key, new string('*', kvp.Key.Length));
                else
                    outPut.Replace(kvp.Key, string.Concat("[", kvp.Value, "]"));
            }
            return outPut.ToString();
        }

        #endregion

        #region 是否含有要禁止的广告词汇

        /// <summary>
        /// 是否含有要禁止的广告词汇
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        public static bool IsContainBanWords(string inPut)
        {
            return MyDAL.GetBanWords().Any(inPut.Contains);
        }

        #endregion

        #region 是否包含汉字

        /// <summary>
        /// 是否包含汉字
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        public static bool IsContainChinese(string inPut)
        {
            return RegexHepler.Chinese.Matches(inPut).Count > 0;
        }

        #endregion

        #region 链接是否超标

        /// <summary>
        /// 链接是否超标
        /// </summary>
        /// <param name="InPut"></param>
        /// <returns></returns>
        public static bool IsLinksNumTooMuch(string InPut)
        {
            int i = Regex.Match(InPut, @"https:|http:").Groups.Count;
            CMSConfig config = CMSConfig.Instance();
            if (i > config.DefendPROC.LinkNum)
            {
                return false;
            }
            return true;
        }

        #endregion

        #region 数据量是否超标

        /// <summary>
        /// 数据量是否超标
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static bool IsDataTooMuch(string tableName)
        {
            string IP = HttpClient.GetIP();
            int i = MyDAL.GetInfoNumIn(IP, tableName);
            CMSConfig config = CMSConfig.Instance();
            if (i > config.DefendPROC.PostSpeed)
            {
                IPBlackList model = new IPBlackList {Id = 0, IPStart = IP, IPEnd = IP, AdminName = "WinnerCMS"};

                new IPBlackListManage().Add(model);

                return true;
            }
            return false;
        }

        #endregion
    }
}